package com.drug.mingkang.controller;

import com.drug.mingkang.model.HqSupplier;
import com.drug.mingkang.service.HqSupplierService;
import com.drug.mingkang.utils.ResponseMessage;
import com.drug.mingkang.utils.Tools;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 供应商(HqSupplier)表控制层
 *
 * @author wenchangjian
 * @since 2020-08-11 12:00:13
 */
@RestController
@RequestMapping("hqSupplier")
public class HqSupplierController {
    /**
     * 服务对象
     */
    @Resource
    private HqSupplierService hqSupplierService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public HqSupplier selectOne(String id) {
        return this.hqSupplierService.queryById(id);
    }

    /**
     * 查询全部
     *
     * @param page
     * @param limit
     * @param supName
     * @return
     */
    @PostMapping("showAll")
    public Map<String, Object> showAll(int page, int limit, String supName,String supPhone) {
        List<HqSupplier> list = hqSupplierService.queryAllByLimit(page, limit, supName,supPhone);
        int count = hqSupplierService.selectCount(supName);
        System.err.println(list);
        return new Tools().limit(list, count);
    }

    /**
     * 删除/{id}
     */
    @DeleteMapping("delete")
    public ResponseMessage delete(String id) {
        //修改状态
        String ids[] = id.split(",");
        return hqSupplierService.update(ids) ? new ResponseMessage().success() : new ResponseMessage().error();
    }

    /**
     * 增加
     */
    @RequestMapping("insert")
    @ResponseBody
    public ResponseMessage insert(HqSupplier hqSupplier) {
        ResponseMessage responseMessage = new ResponseMessage();
        int rows=0;
        if(hqSupplier.getSupId()!= null && !hqSupplier.getSupId().equals("")){
           rows= hqSupplierService.updates(hqSupplier);
        }else{
           rows= hqSupplierService.insert(hqSupplier);
        }

        if (rows>0) {
            responseMessage.setStatus(1);
            responseMessage.setMessage("保存成功");
        } else {
            responseMessage.setStatus(0);
            responseMessage.setMessage("保存失败");
        }
        return responseMessage;
    }

}